﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Configuration;

namespace SPProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (!string.IsNullOrEmpty(args[0]))
                {
                    if (args[0].ToLower().Contains("help"))
                    {
                        Help();
                    }
                    else if (args[0].ToLower().Contains("-list"))
                    {
                        if (args[1].ToLower().Contains("-url"))
                        {
                            if (!string.IsNullOrEmpty(args[2]))
                            {
                                List(args[2]);
                            }
                        }
                        else
                        {
                            Help();
                        }
                    }
                    else if (args[0].ToLower().Contains("-update"))
                    {
                        if (args[1].ToLower().Contains("-url"))
                        {
                            if (!string.IsNullOrEmpty(args[2]) && !string.IsNullOrEmpty(args[4]) && !string.IsNullOrEmpty(args[6]))
                            {
                                Update(args[2], args[4], args[6]);
                            }
                        }
                        else
                        {
                            Help();
                        }
                    }
                    else
                    {
                        Help();
                    }
                }
                else
                {
                    Help();
                }
            }
            catch (Exception ex)
            {
                Help(ex.Message);
            }
        }

        static void Help()
        {
            Console.WriteLine(" ");
            Console.WriteLine("Use SPProperties to list properties of an SPWeb and/or update individual properties. Update will add properties that do not already exist.");
            Console.WriteLine("-list -url http://SharePointUrl");
            Console.WriteLine("-update -url http://SharePointUrl -key KeyName -value Value");
        }
        static void Help(string err)
        {
            Console.WriteLine(" ");
            Console.WriteLine("An error occured: " + err);
            Console.WriteLine("Use SPProperties to list properties of an SPWeb and/or update individual properties. Update will add properties that do not already exist.");
            Console.WriteLine("-list -url http://SharePointUrl");
            Console.WriteLine("-update -url http://SharePointUrl -key KeyName -value Value");
        }
        static void List(string url)
        {
            SPWeb web = null;

           // SPSecurity.RunWithElevatedPrivileges(delegate()
                //{
                    SPSite thisSite = new SPSite(url);
                    web = thisSite.OpenWeb();
                    thisSite.Dispose();
               // });


            try
            {
                web.AllowUnsafeUpdates = true;
                SPPropertyBag props = web.Properties;
                foreach (DictionaryEntry de in props)
                {
                    Console.WriteLine("Key = {0}, Value = {1}", de.Key, de.Value);
                }
            }
            catch (Exception ex)
            {
                Help(ex.Message);
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
                web.Dispose();
            }

        }
        static void Update(string url, string key, string value)
        {
            SPWeb web = null;

            SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPSite thisSite = new SPSite(url);
                    web = thisSite.OpenWeb();
                    thisSite.Dispose();
                });


            try
            {
                web.AllowUnsafeUpdates = true;
                if (web.Properties.ContainsKey(key))
                {
                    web.Properties[key] = value.Trim();
                }
                else
                {
                    web.Properties.Add(key, value);
                }

                web.Properties.Update();
                Console.WriteLine("Updated " + key + " to " + value);
            }
            catch (Exception ex)
            {
                Help(ex.Message);
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
                web.Dispose();
            }
        }

    }     
 }
 